<template>
	<block v-if="types!=3&&types!=4">
		<view style="box-sizing: border-box; padding: 10px;">
			<view v-if="types==1" style="border-bottom: #eee solid 1px;">
				<u-input v-model="form.title" placeholder="请输入标题" :type="type" :border="border" />
			</view>

			<u-input v-model="form.content" placeholder="你今天想说点什么？" type="textarea" :border="border" height="200"
				:auto-height="true" />
			<view>
				<upload @images="images" :getimg="getimg" :videos="form.video" :type="types" :count="length"></upload>
			</view>
			<view style="line-height: 30px;">
				添加{{types==2?'视频':'图片'}}（{{length}}/{{types==2?'1':'9'}}）
			</view>
			<view style="display: flex; line-height: 50px;">
				<view>
					<image style="width: 14px; height: 16px; vertical-align: -3px; margin-right: 8px;"
						src="/static/ren.png"></image>
					谁可查看
				</view>
				<view style="margin-left: auto;" v-if="is_self == 1" @click="is_self = 0">
					仅自己 <u-icon name="arrow-right"></u-icon>
				</view>
				<view style="margin-left: auto;" v-if="is_self == 0" @click="is_self = 1">
					所有人 <u-icon name="arrow-right"></u-icon>
				</view>
			</view>

			<view>
				<view @click="submit"
					style="width: 95%; margin: auto; border-radius: 30px; background: #FA9700; color: #fff; line-height: 46px; text-align: center; position: absolute; z-index: 1; bottom: 20px; left: 2.5%;">
					发布
				</view>
			</view>
		</view>
	</block>
	<block v-else>
		<view style="box-sizing: border-box; padding: 10px;">
			<view>
				<upload @images="images" :getimg="getimg" :videos="video" :type="types" :count="length"></upload>
			</view>
			<view style="line-height: 30px;">
				添加{{types==4?'视频':'图片'}}（{{length}}/{{types==4?'1':'9'}}）
			</view>
			<!-- <view style="display: flex; line-height: 50px;">
				<view>
					<image style="width: 14px; height: 16px; vertical-align: -3px; margin-right: 8px;"
						src="/static/ren.png"></image>
					谁可查看
				</view>
				<view style="margin-left: auto;" v-if="is_self == 1" @click="is_self = 0">
					仅自己 <u-icon name="arrow-right"></u-icon>
				</view>
				<view style="margin-left: auto;" v-if="is_self == 0" @click="is_self = 1">
					所有人 <u-icon name="arrow-right"></u-icon>
				</view>
			</view> -->

			<view>
				<view @click="submitHuihe"
					style="width: 95%; margin: auto; border-radius: 30px; background: #FA9700; color: #fff; line-height: 46px; text-align: center; position: absolute; z-index: 1; bottom: 20px; left: 2.5%;">
					上传
				</view>
			</view>
		</view>
	</block>

</template>

<script>
	import upload from '@/common/unload.vue'
	import {
		newspush,
		addPhoto,
		lookDraft,
		saveDraft
	} from "@/common/config/request.js"
	// import  from 
	export default {
		components: {
			upload
		},
		data() {
			return {
				form: {},
				length: 0,
				type: 'text',
				value: '',
				border: false,
				types: null,
				uptype: '',
				image: [],
				getimg: [],
				is_self: 0,
				video:"",
			}
		},
		onLoad(e) {
			this.types = e.type
			console.log(this.types, 'this.types')
			if (e.type == 0) {
				uni.setNavigationBarTitle({
					title: '发布短讯'
				})
			}
			if (e.type == 1) {
				uni.setNavigationBarTitle({
					title: '发布文章'
				})
			}
			if (e.type == 2) {
				uni.setNavigationBarTitle({
					title: '发布视频'
				})
			}
			if (e.type == 3) {
				uni.setNavigationBarTitle({
					title: '上传图片'
				})
			}
			if (e.type != 3) {
				this.getDraft()
			}
		},
		onShow() {

		},
		onBackPress(btnType) {
			console.log(btnType,'btnTypebtnType')
			if (btnType.from === "backbutton") {	
				if (this.types != 3) {
					let params = {
						type: this.types
					}
					if (this.form.title !== '') {
						params.title = this.form.title
					}
					if (this.form.content !== '') {
						params.content = this.form.content
					}
					if (this.form.images !== '' && this.types != 2) {
						params.images = this.image.toString()
					}
					if (this.form.video !== '' && this.types == 2) {
						params.video = this.image.toString()
					}
					// console.log(params, '444444444')
					saveDraft(params).then(res => {
						console.log(res, '222222')
						if (res.data.code === 1) {
							return true
						}
					})
				}
			}
		},
		methods: {
			images(e) {
				console.log(e,'666666555555555555555333333333333')
				this.length = e.length
				this.image = e
			},
			submit() {
				this.form.type = this.types
				if (this.types != 2) {
					this.form.images = this.image.toString()
				} else {
					this.form.video = this.image.toString()
				}
				this.form.is_self = this.is_self
				newspush(this.form).then(res => {
					this.queryInfo.queryInfo(res.data.code, 'formdata', '')
				})
			},
			submitHuihe() {
				if (this.types == 3) {
					this.form.images = this.image.toString()
				}
				addPhoto(this.form).then(res => {
					this.queryInfo.queryInfo(res.data.code, 'formdata', '')
				})
			},
			// 查看草稿
			getDraft() {
				let params = {
					type: this.types
				}
				lookDraft(params).then(res => {
					console.log(res.data.data, '2222222222222')
					if (res.data.code == 1) {
						this.$set(this.form, 'content', res.data.data.content)
						this.$set(this.form, 'title', res.data.data.title)
						this.$set(this.form, 'images', res.data.data.images)
						this.$set(this.form, 'video', res.data.data.video)
						if (this.types != 2) {
							if(this.form.images){
								this.getimg = this.form.images.split(',')
								this.image=this.form.images.split(',')
							}
							
						} else {
							if(this.form.video){
								// this.image=this.form.images.split(',')
								this.getimg = this.form.video.split(',')
								this.image = this.form.video
							}
							
						}
					}
					// if (res.data.code === 1) {
					// 	// if (res.data.data.id) {
					// 	// 	console.log('进入1')
					// 	// 	if (this.types == 0) {
					// 	// console.log('进入2', res.data.data)
					// 	// console.log(this.form)
					// 	this.$set(this.form, 'content', res.data.data.content)
					// 	this.$set(this.form, 'video', res.data.data.video)
					// 	this.$set(this.form, 'title', res.data.data.title)
					// 	this.$set(this.form, 'images', res.data.data.images)
					// 	// console.log(this.form.images)
					// 	if(this.types!=2){
					// 		this.getimg = this.form.images.split(',')
					// 	}else{
					// 		// this.image=this.form.images.split(',')
					// 		this.getimg = this.form.video.split(',')
					// 	}
					// 	// console.log(this.getimg,'this.getimgthis.getimgthis.getimg')
					// 	// this.form.content = res.data.data.content
					// 	// this.form.images = res.data.data.images
					// 	// 	}
					// 	// }
					// }
				})
			}
		},

	}
</script>

<style>

</style>
